package com.xxxx.ssm.controller;

import com.xxxx.ssm.BaseController;
import com.xxxx.ssm.po.Account;
import com.xxxx.ssm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 对RestFul做总结
 * ResutFul Url配置
 *  1.设置请求类型
 *      GET [查询] @GetMapping
 *      DELETE [删除] @DeleteMapping
 *      POST [添加] @PostMapping
 *      PUT [更新] @PutMapping
 *  2.URL设置的时候,不体现动作行为(没有动词)
 *      例如:/account/1 , /account/2/ , /account
 *  3.定义参数格式
 *      1.路径参数(@PathVariable)
 *      2.json格式(@RequestBody)
 *      3.普通表单参数
 *  4.设置响应数据
 *  json格式(ResponseBody)
 *
 */

@Controller
public class AccountController extends BaseController {
    //控制器要调用Service层的数据

    @Autowired
    private AccountService accountService;


    /**
     * 查询操作
     *  传统的URL访问
     *      @RequestMapping("/account/queryAccountById")
     *      http://localhost:8080/ssm/account/queryAccountById?id = 1
     *      使用传统的方式不用再参数前面声明@PathVariable
     *  ResultFul访问
     * @GettingMapping("/account/{id}")
     *      http://localhost:8080/ssm/account/1
     *      使用ResultFul需要再参数前面声明@PathVariable
     *
     *@PathVraiable 将形参设置为参数路径,声明再参数前面
     * @param id
     * @return
     */

    //使用原始方式进行定义
//    @RequestMapping("/account/queryAccountById")
    @GetMapping("/account/{id}")
    @ResponseBody
    public Account queryAccountById(@PathVariable Integer id){
        //系统异常会被捕获
        int i = 1 / 0;

        //自定义异常能否被捕获
        //答案：自定义异常抛出的时候也能被配置的异常处理器捕获
        if(1 == 1){
//            throw new ParamsException();
//            throw new BusinessException();
        }

        return accountService.selectById(id);
    }



    @DeleteMapping("/account/{id}")
    @ResponseBody
    public Map<String , String> deleteAccountById(@PathVariable Integer id){
        //调用service层的删除方法,返回受影响的行数
        int row = accountService.delAccount(id);
        //判断受影响的函数是否大于0
        HashMap<String, String> map = new HashMap<>();
        if(row > 0){
            //说明删除成功
            map.put("code" , "200");
            map.put("msg" , "删除成功");
        }else{
            //说明删除失败
            map.put("code" , "500");
            map.put("msg" , "删除失败");
        }
        return map;
    }


    @PostMapping("/account")
    @ResponseBody
    public Map<String , String> addAccount(@RequestBody Account account){
        Map<String , String> map =  new HashMap<>();
        //调用Service层的添加方法
        Integer row = accountService.saveAccount(account);
        if(row > 0){
            //账户信息添加成功
            map.put("code" , "200");
            map.put("msg" , "添加成功");
        }else{
            //账户信息添加失败
            map.put("code" , "500");
            map.put("msg" , "添加失败");
        }
        return map;
    }


    @PutMapping("/account")
    @ResponseBody
    public Map<String , String> updateAccount(@RequestBody Account account){
        //定义返回的结果变量
        HashMap<String, String> map = new HashMap<>();
        //调用服务层的修改方法
        int row = accountService.updateAccount(account);
        if(row > 0){
            //修改成功
            map.put("code" , "200");
            map.put("msg" , "修改成功");
        }else{
            //修改失败
            map.put("code" , "500");
            map.put("msg" ,"修改失败");
        }
        return map;
    }




}
